// pages/case/case.js
import API from "../../api/index";
Page({
  data: {
    scrollTop:0,
    navList:['线上案例','线下案例'],
    navIndex:0, // 顶部列表下标
    oldIndex:0, // 上一次列表下标
    firstShow:true,
    business_list:[],
    footer:true, // 是否置底了
    itemList: [
      {
        banner: '/static/images/case01.png',
        pic: [
          {img:'/static/images/left1.png',date: '2019',title:'厉川美术馆官网'},
          {img:'/static/images/left3.png',date: '2019',title:'騰訊智慧出行'},
          {img: '/static/images/left5.png',date: '2018',title:'爱易定制小程序'},
          {img: '/static/images/left2.png',date: '2019',title: '厉川美术馆小程序'},
          {img: '/static/images/left4.png',date: '2016',title: '傍大款小程序'},
          {img: '/static/images/left6.png',date: '2017',title: '庐山小程序'}
        ]
      },
      {
        banner: '/static/images/case02.png',
        pic: [
          {img: '/static/images/right1.png',date: '2018',title: '中式空间设计--客厅'},
          {img: '/static/images/right3.png',date: '2018',title: '产品包装设计'},
          {img: '/static/images/right5.png',date: '2018',title: '展览设计'},
          {img: '/static/images/right2.png',date: '2018',title: '产品包装设计'},
          {img: '/static/images/right4.png',date: '2018',title: 'VI设计'},
          {img: '/static/images/right6.png',date: '2018',title: '展览设计'}
        ]
      }
    ],
    page: 1,
    page_size: 5
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._getData()
    this.setData({ firstShow: true })
  },
  _getData() {
    API.getCaseList(Number(this.data.navIndex+1)).then(res => {
      if(res.code==200){
        let list = res.data.cases;
        list.forEach(obj => {
          obj.time = obj.case_name.substr(0, 4);
          Number(obj.time) > 0 ? obj.time + '年' : obj.time;
          obj.case_name = obj.case_name.substr(4);
        })
        this.setData({ business_list: [...this.data.business_list, ...list], footer:true})
      } else if (res.code == 10000){
        this.setData({business_list:[]})
      }else{
        this.setData({ footer: false })
      }
    })
  },
  bindchange(e){
    this.setData({
      currentData:e.detail.current
    })
  },
  checkCurrent(e){
    if (e.target.dataset.index != this.data.navIndex) {
      this.setData({
        page:1,
        navIndex: e.target.dataset.index,
        oldIndex: e.target.dataset.index==0?1:0
      })
      this._getData()
      if (this.data.firstShow){ // 非首次加载判断
        this.setData({ firstShow: false })
      }
    }
  },
  onPageScroll(e){
    this.setData(e)
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      business_list: []
    })
    this._getData()
    wx.stopPullDownRefresh()
  },
  // 上拉触底
  onReachBottom: function () {
    this._getData()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})